﻿
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Yz.Base;
using Yz.Cache;
using Yz.Cache.Base;
using Yz.Core.Base;
using Yz.DataAccess.DataCache.Interface;
using Yz.DataAccess.DbAccessses.Interface;
using Yz.Model.Entities;

namespace Yz.DataAccess.DataCache
{
    public class UserCache : BaseCache, IUserCache
    {
        private readonly int dbNum = RedisDbNum.System;
        private readonly ICacheUitility _cacheUitility;
        private readonly IUserAccess _userAccess;
        public UserCache(IUserAccess userAccess, ICacheUitility cacheUitility)
        {
            _userAccess = userAccess;
            _cacheUitility = cacheUitility;
        }
        /// <summary>
        ///  
        /// </summary>
        /// <returns></returns>
        public ReturnList<T> GetListByPage<T>(Expression<Func<User, bool>> where = null, ParaPage paraPage = null, List<ParaOrder<User>> orders = null, bool hasTotal = true, bool toUpdate = false) where T : ViewModel
        {
            var F = new Func<Expression<Func<User, bool>>, ParaPage, List<ParaOrder<User>>, bool, ReturnList<T>>(_userAccess.GetListByPage<T>);
            var list = _cacheUitility.GetListFromCache(F, where, paraPage, orders, hasTotal, new CacheDbTime() { Time = 1, TimeType = TimeType.Day, DbNum = dbNum }, toUpdate);
            return list;
        }

        /// <summary>
        ///  
        /// </summary>
        /// <returns></returns>
        public async Task<ReturnList<T>> GetListByPageAsync<T>(Expression<Func<User, bool>> where = null, ParaPage paraPage = null, List<ParaOrder<User>> orders = null, bool hasTotal = true, bool toUpdate = false) where T : ViewModel
        {
            var F = new Func<Expression<Func<User, bool>>, ParaPage, List<ParaOrder<User>>, bool, Task<ReturnList<T>>>(_userAccess.GetListByPageAsync<T>);
            var list = await _cacheUitility.GetListFromCacheAsync(F, where, paraPage, orders, hasTotal, new CacheDbTime() { Time = 1, TimeType = TimeType.Day, DbNum = dbNum }, toUpdate);
            return list;
        }
    }
}
